package oa.hleast.xswl.mapper.pms;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author 00209
 * @Date 2021/03/01 22:01:45
 */
@Mapper
public interface PmsSpuMapper extends BaseMapper<PmsSpu> {

    @Select("<script>" +
            " SELECT  " +
            " t1.*," +
            " t2.NAME AS category_name ," +
            " t3.NAME AS brand_name " +
            " FROM " +
            "  pms_spu t1" +
            " LEFT JOIN pms_category t2 ON t1.category_id = t2.id " +
            " LEFT JOIN pms_brand t3 ON t1.brand_id = t3.id " +
            " WHERE 1=1 " +
            " <if test ='spu.categoryId !=null and spu.categoryId > 0' >" +
            "       AND t2.id = #{spu.categoryId} " +
            "       And t1.status = 0 " +
            " </if>" +
            " ORDER BY t1.gmt_create DESC" +
            "</script>")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "skuList", column = "id", many = @Many(select = "oa.hleast.xswl.mapper.pms.PmsSkuMapper.listBySpuId"))
    })
    List<PmsSpu> list(@Param("page") Page<PmsSpu> page, @Param("spu") PmsSpu spu);

    @Select("<script>" +
            " SELECT  " +
            " t1.*," +
            " t2.NAME AS category_name ," +
            " t3.NAME AS brand_name " +
            " FROM " +
            "  pms_spu t1" +
            " LEFT JOIN pms_category t2 ON t1.category_id = t2.id " +
            " LEFT JOIN pms_brand t3 ON t1.brand_id = t3.id " +
            " WHERE 1=1 " +
            "       And t1.status = #{spu.status} " +
            " <if test ='spu.categoryId !=null and spu.categoryId > 0' >" +
            "       AND t2.id = #{spu.categoryId} " +
            " </if>" +
            " ORDER BY t1.gmt_create DESC" +
            "</script>")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "skuList", column = "id", many = @Many(select = "oa.hleast.xswl.mapper.pms.PmsSkuMapper.listBySpuId"))
    })
    List<PmsSpu> listByStatus(@Param("page") Page<PmsSpu> page, @Param("spu") PmsSpu spu);

    /**
     * @param page
     * @param spu
     * @return List<PmsSpu>
     * 返回在售商品的信息
     */
    @Select("<script>" +
            " SELECT  " +
            " t1.*," +
            " t2.NAME AS category_name ," +
            " t3.NAME AS brand_name " +
            " FROM " +
            "  pms_spu t1" +
            " LEFT JOIN pms_category t2 ON t1.category_id = t2.id " +
            " LEFT JOIN pms_brand t3 ON t1.brand_id = t3.id " +
            " WHERE 1=1 and t1.status = 1" +
            " <if test ='spu.categoryId !=null and spu.categoryId > 0' >" +
            "       AND t2.id = #{spu.categoryId} " +
            " </if>" +
            " ORDER BY t1.gmt_create DESC" +
            "</script>")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "skuList",column = "id",many = @Many(select="oa.hleast.xswl.mapper.pms.PmsSkuMapper.listBySpuId"))
    })
    List<PmsSpu> inStockList(@Param("page") Page<PmsSpu> page, @Param("spu") PmsSpu spu);
}
